//
//  DataHandle.m
//  FNer
//
//  Created by 王展 on 15/9/29.
//  Copyright (c) 2015年 wangzhan. All rights reserved.
//

#import "DataHandle.h"
#import <sqlite3.h>


@implementation DataHandle

+(instancetype)shareDataHandle
{
    static DataHandle *dataHandle = nil;
    if (dataHandle == nil) {
        dataHandle = [[DataHandle alloc] init];
    }
    return dataHandle;
}


-(void)huanCun:(NSMutableDictionary *)dic
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSLog(@"documentPath = %@",documentPath);
    
    self.rootPath = [documentPath stringByAppendingPathComponent:@"HuancunDic.plist"];
    
    BOOL result = [NSKeyedArchiver archiveRootObject:dic toFile:self.rootPath];
    if (result) {
        NSLog(@"写入字典成功");
    }
    else
    {
        NSLog(@"写入字典失败");
    }
}

-(void)communityHuanCun:(NSMutableDictionary *)dic
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSLog(@"documentPath = %@",documentPath);
    
    NSString *huancunDicFilePath = [documentPath stringByAppendingPathComponent:@"CommunityHuancunDic.plist"];
    BOOL result = [NSKeyedArchiver archiveRootObject:dic toFile:huancunDicFilePath];
    if (result) {
        NSLog(@"写入字典成功");
    }
    else
    {
        NSLog(@"写入字典失败");
    }
}


-(void)videoHuanCun:(NSMutableDictionary *)dic
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSLog(@"documentPath = %@",documentPath);
    
    NSString *huancunDicFilePath = [documentPath stringByAppendingPathComponent:@"videoDic.plist"];
    BOOL result = [NSKeyedArchiver archiveRootObject:dic toFile:huancunDicFilePath];
    if (result) {
        NSLog(@"写入字典成功");
    }
    else
    {
        NSLog(@"写入字典失败");
    }
}

-(void)lunboHuanCun:(NSMutableDictionary *)dic
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSLog(@"documentPath = %@",documentPath);
    
    NSString *huancunDicFilePath = [documentPath stringByAppendingPathComponent:@"LunboDic.plist"];
    BOOL result = [NSKeyedArchiver archiveRootObject:dic toFile:huancunDicFilePath];
    if (result) {
        NSLog(@"写入字典成功");
    }
    else
    {
        NSLog(@"写入字典失败");
    }
}

-(void)CommunityDetailHuanCun:(NSMutableDictionary *)dic
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSLog(@"documentPath = %@",documentPath);
    
    NSString *huancunDicFilePath = [documentPath stringByAppendingPathComponent:@"CommunityDetailDic.plist"];
    BOOL result = [NSKeyedArchiver archiveRootObject:dic toFile:huancunDicFilePath];
    if (result) {
        NSLog(@"写入字典成功");
    }
    else
    {
        NSLog(@"写入字典失败");
    }
}

-(void)OtherHuancun:(NSMutableDictionary *)dic
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSLog(@"documentPath = %@",documentPath);
    
    NSString *huancunDicFilePath = [documentPath stringByAppendingPathComponent:@"OtherDic.plist"];
    BOOL result = [NSKeyedArchiver archiveRootObject:dic toFile:huancunDicFilePath];
    if (result) {
        NSLog(@"写入字典成功");
    }
    else
    {
        NSLog(@"写入字典失败");
    }
}

-(void)informationHuancun:(NSMutableDictionary *)dic
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSLog(@"documentPath = %@",documentPath);
    
    NSString *huancunDicFilePath = [documentPath stringByAppendingPathComponent:@"informationDic.plist"];
    BOOL result = [NSKeyedArchiver archiveRootObject:dic toFile:huancunDicFilePath];
    if (result) {
        NSLog(@"写入字典成功");
    }
    else
    {
        NSLog(@"写入字典失败");
    }
}

//数据库
static sqlite3 *db = nil;
//创建数据库
-(void)openDB
{
    if (db != nil) {
       // NSLog(@"数据库已经打开");
        return;
    }
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [array lastObject];
    NSString *sqlFilePath = [filePath stringByAppendingPathComponent:@"channel.sqlite"];
    NSLog(@"----------%@",sqlFilePath);
    int result = sqlite3_open(sqlFilePath.UTF8String, &db);
    if (result == SQLITE_OK) {
       // NSLog(@"打开数据库成功");
    }
    else
    {
       // NSLog(@"打开数据库失败");
    }
}
//创建表
-(void)createChannelTable
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS channel(number INTEGER PRIMARY KEY AUTOINCREMENT, channelDesc TEXT, channelIcon TEXT, channelName TEXT, id INTEGER)";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
       // NSLog(@"创建表成功");
    }
    else
    {
       // NSLog(@"创建表失败");
    }
}
//插入表
-(void)insertFilmTable:(Channels *)channel
{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO channel(channelDesc, channelIcon, channelName, id) VALUES('%@', '%@', '%@', '%ld')",channel.channelDesc,channel.channelIcon,channel.channelName,channel.id];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
       // NSLog(@"插入成功");
    }
    else
    {
       // NSLog(@"插入失败");
    }
}
//查询表
-(NSMutableDictionary *)selectChannel
{
    NSMutableArray *array = [NSMutableArray array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    NSString *sql = @"SELECT * FROM channel";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare(db, sql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
       // NSLog(@"查询准备成功");
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *channelDesc = sqlite3_column_text(stmt, 1);
            const unsigned char *channelIcon = sqlite3_column_text(stmt, 2);
            const unsigned char *channelName = sqlite3_column_text(stmt, 3);
            int id = sqlite3_column_int(stmt, 4);
            
            NSString *channelDescStr = [NSString stringWithUTF8String:(const char *)channelDesc];
            NSString *channelIconStr = [NSString stringWithUTF8String:(const char *)channelIcon];
            NSString *channelNameStr = [NSString stringWithUTF8String:(const char *)channelName];
            NSInteger idInt = id;
            Channels *achannel = [[Channels alloc]init];
            achannel.channelDesc = channelDescStr;
            achannel.channelIcon = channelIconStr;
            achannel.channelName = channelNameStr;
            achannel.id = idInt;
            [array addObject:achannel];
            [dic setObject:achannel forKey:achannel.channelName];
        }
        
    }
    else
    {
        //NSLog(@"查询准备失败");
    }
    
    [dic setObject:array forKey:@"array"];
    
    return dic;
}
//删除订阅
-(void)deleteChannel:(NSString *)channelName
{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM channel WHERE channelName = '%@'",channelName];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
       // NSLog(@"删除成功");
    }
    else
    {
        //NSLog(@"删除失败");
    }
}


//音乐收藏
//创建表
-(void)createMusicTable
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS music(number INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, pic TEXT, source TEXT, id TEXT)";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        //NSLog(@"创建表成功");
    }
    else
    {
        //NSLog(@"创建表失败");
    }
}
//插入表
-(void)insertMusicTable:(Music *)music
{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO music(name, pic, source, id) VALUES('%@', '%@', '%@', '%@')",music.name,music.pic,music.source,music.id];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
       // NSLog(@"插入成功");
    }
    else
    {
       // NSLog(@"插入失败");
    }
}
//删除音乐
-(void)deleteMusic:(NSString *)musicName
{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM music WHERE name = '%@'",musicName];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
       // NSLog(@"删除成功");
    }
    else
    {
       // NSLog(@"删除失败");
    }
}
//查询音乐
-(NSMutableDictionary *)selectMusic
{
    NSMutableArray *array = [NSMutableArray array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    NSString *sql = @"SELECT * FROM music";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare(db, sql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
       // NSLog(@"查询准备成功");
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *pic = sqlite3_column_text(stmt, 2);
            const unsigned char *source = sqlite3_column_text(stmt, 3);
            const unsigned char *id = sqlite3_column_text(stmt, 4);
            
            NSString *nameStr = [NSString stringWithUTF8String:(const char *)name];
            NSString *picStr = [NSString stringWithUTF8String:(const char *)pic];
            NSString *sourceStr = [NSString stringWithUTF8String:(const char *)source];
            NSString *idStr = [NSString stringWithUTF8String:(const char *)id];
            Music *amusic = [[Music alloc]init];
            amusic.name = nameStr;
            amusic.pic = picStr;
            amusic.source = sourceStr;
            amusic.id = idStr;
            [array addObject:amusic];
            [dic setObject:amusic forKey:amusic.name];
        }
        
    }
    else
    {
       // NSLog(@"查询准备失败");
    }
    
    [dic setObject:array forKey:@"array"];
    
    return dic;
}

//视频收藏
//创建视频表
-(void)createVideoTable
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS video(number INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, coverForFeed TEXT, playUrl TEXT, category TEXT, detail TEXT, coverBlurred TEXT, id INTEGER)";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
      //  NSLog(@"创建表成功");
    }
    else
    {
      //  NSLog(@"创建表失败");
    }
}
//插入视频
-(void)insertVideoTable:(Video *)avideo
{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO video(title, coverForFeed, playUrl, category, detail, coverBlurred, id) VALUES('%@', '%@', '%@', '%@', '%@', '%@', '%ld')",avideo.title,avideo.coverForFeed,avideo.playUrl,avideo.category,avideo.detail,avideo.coverBlurred,avideo.id];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
       // NSLog(@"插入成功");
    }
    else
    {
       // NSLog(@"插入失败");
    }
}
//删除视频
-(void)deleteVideo:(NSString *)videoName
{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM video WHERE title = '%@'",videoName];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
       // NSLog(@"删除成功");
    }
    else
    {
       // NSLog(@"删除失败");
    }
}
//查询视频
-(NSMutableDictionary *)selectVieo
{
    NSMutableArray *array = [NSMutableArray array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    NSString *sql = @"SELECT * FROM video";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare(db, sql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        //NSLog(@"查询准备成功");
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            const unsigned char *coverForFeed = sqlite3_column_text(stmt, 2);
            const unsigned char *playUrl = sqlite3_column_text(stmt, 3);
            const unsigned char *category = sqlite3_column_text(stmt, 4);
            const unsigned char *detail = sqlite3_column_text(stmt, 5);
            const unsigned char *coverBlurred = sqlite3_column_text(stmt, 6);
            int id = sqlite3_column_int(stmt, 7);
            
            NSString *titleStr = [NSString stringWithUTF8String:(const char *)title];
            NSString *coverForFeedStr = [NSString stringWithUTF8String:(const char *)coverForFeed];
            NSString *playUrlStr = [NSString stringWithUTF8String:(const char *)playUrl];
            NSString *categoryStr = [NSString stringWithUTF8String:(const char *)category];
            NSString *detailStr = [NSString stringWithUTF8String:(const char *)detail];
            NSString *coverBlurredStr = [NSString stringWithUTF8String:(const char *)coverBlurred];
            NSInteger idInt = id;
            
            Video *avideo = [[Video alloc]init];
            avideo.title = titleStr;
            avideo.coverForFeed = coverForFeedStr;
            avideo.playUrl = playUrlStr;
            avideo.category = categoryStr;
            avideo.detail = detailStr;
            avideo.coverBlurred = coverBlurredStr;
            avideo.id = idInt;
            [array addObject:avideo];
            [dic setObject:avideo forKey:avideo.title];
        }
        
    }
    else
    {
       // NSLog(@"查询准备失败");
    }
    
    [dic setObject:array forKey:@"array"];
    
    return dic;
}

-(void)createMovieTable
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS movie(number INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, coverForFeed TEXT, playUrl TEXT, descriPtion TEXT)";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }
    else
    {
        NSLog(@"创建表失败");
    }
}
-(void)insertMovieTable:(MovieModel *)movie
{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO movie(title, coverForFeed, playUrl, description) VALUES('%@', '%@', '%@', '%@')",movie.title,movie.coverForFeed,movie.playUrl,movie.descriPtion];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
         NSLog(@"插入成功");
    }
    else
    {
         NSLog(@"插入失败");
    }
}
-(NSMutableArray *)selectMovie
{
    NSMutableArray *allMovieArray = [NSMutableArray array];
    
    NSString *sql = @"SELECT * FROM movie";
    
    sqlite3_stmt * stmt= nil;
    
    
    
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, NULL);
    
    if (result == SQLITE_OK) {
        // NSLog(@"查询准备成功");
        //遍历表中每一行
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            //取出某一行中第一列 数据
            //第一例下标0
            //取出某一行的第二列
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            
            const unsigned char *coverForFeed = sqlite3_column_text(stmt, 2);
            
            const unsigned char *playUrl = sqlite3_column_text(stmt, 3);
            
            const unsigned char *description = sqlite3_column_text(stmt, 4);
            
            NSString *titleStr = [NSString stringWithUTF8String:(const  char *)title];
            
            NSString *coverForFeedStr = [NSString stringWithUTF8String:(const  char *)coverForFeed];
            
            NSString *playUrlStr = [NSString stringWithUTF8String:(const  char *)playUrl];
            
            NSString *descriptionStr = [NSString stringWithUTF8String:(const  char *)description];
          
            
            //  NSLog(@"title = %@ digest = %@ imgstr = %@",titleStr,digestStr,imgsrcStr);
            
            MovieModel *model = [[MovieModel alloc] init];
            model.title = titleStr;
            model.coverForFeed = coverForFeedStr;
            model.playUrl = playUrlStr;
            model.descriPtion = descriptionStr;
           
            [allMovieArray addObject:model];

        }
    }
    else
    {
          NSLog(@"查询准备失败");
    }
    
    return allMovieArray;

}
-(void)deleteMovie:(NSString *)movieName
{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM movie WHERE title = '%@'",movieName];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
         NSLog(@"123456789删除成功");
    }
    else
    {
         NSLog(@"删除失败");
    }
}



@end
